﻿using System;
using System.Collections.Generic;

namespace Loki.Model
{
	public class EntityEqualityComparer<TEntityClass, TEntityKey> : EqualityComparer<TEntityClass> where TEntityClass: IAtomicEntity<TEntityKey>
	{

		public override bool Equals(TEntityClass x, TEntityClass y)
		{
			if (x == null || y == null)
				return x == null;
			else
				return Object.Equals(x.ID, y.ID);
		}

		public override int GetHashCode(TEntityClass obj)
		{
			return obj.ID.GetHashCode();
		}
	}

	public class LinkEqualityComparer<TEntityKey> : EqualityComparer<IEntityLink<TEntityKey>>
	{
		public override bool Equals(IEntityLink<TEntityKey> x, IEntityLink<TEntityKey> y)
		{
			if (x == null || y == null)
				return x == null;
			else
			{
				return Object.Equals(x.Left, y.Left) && Object.Equals(x.Right, y.Right);
			}
		}

		public override int GetHashCode(IEntityLink<TEntityKey> obj)
		{
			if (obj.Left == null && obj.Left == null)
				return 0;
			else if (obj.Right == null)
				return obj.Left.GetHashCode();
			else
				return obj.Left.GetHashCode() ^ obj.Right.GetHashCode();
		}
	}
}